package com.myteam.mystory.fragment;

import java.util.List;

import com.myteam.mystory.R;
import com.myteam.mystory.adapter.HistoryAdapter;
import com.myteam.mystory.adapter.TopViewAdapter;
import com.myteam.mystory.presention.ChapterActivity;
import com.myteam.mystory.quickaction.ActionItem;
import com.myteam.mystory.quickaction.QuickAction;
//import com.myteam.mystory.adapter.AllStoryAdapter;
import com.myteam.mystory.service.StoriesService;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class TopViewFragment extends Fragment{

	private ListView liststory;
	private String[] topview_namestory;
	
	private int mSelectedRow = 0;
	private ImageView mMoreIv = null;
	private static final int ID_ADD = 1;
	private static final int ID_ACCEPT = 2;
	private static final int ID_UPLOAD = 3;

	public TopViewFragment() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View rootView = inflater.inflate(R.layout.topview, container, false);
		liststory = (ListView) rootView.findViewById(R.id.topview_liststory);
		TopViewAdapter adapter = new TopViewAdapter(getActivity());
		topview_namestory = new String[]{"adfasgf","agđgadf","agfdsagag","agfhagfd","agfag","agfah","agfahg","agah","aghadhf","agfah","agđgadf","agfdsagag","agfhagfd","agfag","agfah","agfahg","agah","aghadhf","agfah","agđgadf","agfdsagag","agfhagfd","agfag","agfah","agfahg","agah","aghadhf","agfah"};
		adapter.setData(topview_namestory);
		liststory.setAdapter(adapter);
		
		ActionItem likeCount = new ActionItem("100 like", "  Adjhkfajjgasgfeasur iyauryaif dhasufryar ajfgru sayfeaaiusfd asjgufiarga", 5);
		final QuickAction mQuickAction 	= new QuickAction(getActivity());
        mQuickAction.addActionItem(likeCount);
        
        mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
			@Override
			public void onItemClick(QuickAction quickAction, int pos, int actionId) {
				ActionItem actionItem = quickAction.getActionItem(pos);
				
				if (actionId == ID_ADD) { //Add item selected
					Toast.makeText(getActivity(), "Add item selected on row " + mSelectedRow, Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getActivity(), actionItem.getTitle() + " item selected on row " 
							+ mSelectedRow, Toast.LENGTH_SHORT).show();
				}	
			}
		});
		
		mQuickAction.setOnDismissListener(new PopupWindow.OnDismissListener() {			
			@Override
			public void onDismiss() {
				mMoreIv.setImageResource(R.drawable.bookmark_none);
			}
		});
		
		liststory.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mSelectedRow = position; //set the selected row
				mQuickAction.show(view);
				mMoreIv = (ImageView) view.findViewById(R.id.topview_bookmark);
			}
		});
		return rootView;
	}

}
